package com.android.youwenchuang.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.Nullable;

import com.android.youwenchuang.App;
import com.android.youwenchuang.base.BaseActivity;
import com.android.youwenchuang.bean.User;
import com.android.youwenchuang.databinding.ActivityLoginBinding;
import com.android.youwenchuang.gen.UserDao;
import com.android.youwenchuang.util.UserData;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.CollectionUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.util.List;

public class LoginActivity extends BaseActivity {

    private ActivityLoginBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        //买
        binding.tvMaijia.setOnClickListener(view -> {
            if (TextUtils.isEmpty(binding.etText.getText())) {
                ToastUtils.showShort("请输入账号");
                return;
            }
            List<User> user = App.mApp.getDaoSession().getUserDao().queryBuilder().where(UserDao.Properties.Name.eq(binding.etText.getText())).list();
            if (CollectionUtils.isEmpty(user)) {
                User user1 = new User();
                user1.setName(binding.etText.getText().toString());
                UserData.user.setId(App.mApp.getDaoSession().getUserDao().insert(user1));
            } else {
                UserData.user.setId(user.get(0).getId());
            }
            UserData.user.name = binding.etText.getText().toString();
            UserData.user.isAdmin = true;
            ActivityUtils.startActivity(MainActivity.class);
            finish();
        });
        //卖
        binding.tvMajia.setOnClickListener(view -> {
            if (TextUtils.isEmpty(binding.etText.getText())) {
                ToastUtils.showShort("请输入账号");
                return;
            }
            List<User> user = App.mApp.getDaoSession().getUserDao().queryBuilder().where(UserDao.Properties.Name.eq(binding.etText.getText())).list();
            if (CollectionUtils.isEmpty(user)) {
                User user1 = new User();
                user1.setName(binding.etText.getText().toString());
                UserData.user.setId(App.mApp.getDaoSession().getUserDao().insert(user1));
            } else {
                UserData.user.setId(user.get(0).getId());
            }
            UserData.user.name = binding.etText.getText().toString();
            UserData.user.isAdmin = false;
            ActivityUtils.startActivity(MainActivity.class);
            finish();
        });

    }
}
